var baseUrl = require('../../utils/urllink.js')
var app = getApp();
var sliderWidth = 187.5; // 需要设置slider的宽度，用于计算中间位置
// var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    images: [],
    goods_id: '',
    is_show: false,
    goods_list:[],
    agent_type: 0,
    shop_id:0,
    page:1,
    page_size:10,
    hostimgs: app.globalData.hostimgs,
    shop_avatar:'/imgs/no_head.jpg',
    shop_name:'',
    shop_desc:'',
    collect_sum:0,
    goods_count:0,
  },

  //返回顶部
  scrolltop: function () {
    this.setData({
      scrolltop: 0
    })
  },

  onLoad: function (options) {
    var that = this
    this.setData({
      agent_type: app.globalData.agent_type,
      shop_id: app.globalData.shop_id,
    })

    //获取店铺信息
    getshop(that, app.globalData.user_id) //that.globalData.user_id

  },
  onShow: function () {

  },

  //跳转到商品详情页面
  todetail:function(e){
    var goods_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../goods/detail?goods_id=' + goods_id,
    })
  },
  //预览图片
  previewImage: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    var files = []
    files[0] = id
    wx.previewImage({
      current: id, // 当前显示图片的http链接
      urls: files // 需要预览的图片http链接列表
    })

  },

  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      page: 1,
    })

    // 获取店铺集合 刷新
    goods_list(that, that.data.shop_id, 1, that.data.page_size) 
    wx.showToast({
      title: '刷新',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 获取店铺集合
    goods_list(that, that.data.shop_id, that.data.page, that.data.page_size) 
  },



});






//查询 店铺商品集合
function goods_list(that, shop_id, page, page_size) {
  wx.showLoading({
    title: '加载中...',
  })
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/ShopGoods/goods_list.html',

    data: {
      shop_id: shop_id,
      page: page,
      page_size, page_size,
      type:1,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (page == 1) {
        if (res.data.code == -1) {
          wx.showModal({
            title: '提示',
            content: '您还没有提交店铺资料，请去完善资料吧！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.switchTab({
                  url: '../mine/mine',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          that.setData({
            is_show: true,
          })
          wx.hideLoading();
        } else if (res.data.code == -2) {
          that.setData({
            is_show: true,
          })
          wx.hideLoading();
          
        } else {
          //第一次
          that.setData({
            goods_list: res.data,
            page: page + 1,
            is_show: false,
          })
          wx.hideLoading();
        }
      } else {
        if (res.data.code == -1) {
          wx.showModal({
            title: '提示',
            content: '您还没有提交店铺资料，请去完善资料吧！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.switchTab({
                  url: '../mine/mine',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          wx.showToast({
            title: '没有数据了',
            icon: 'success',
            duration: 2000
          })
        } else if (res.data.code == -2) {
          that.setData({
            is_show: false,
          })
          wx.showToast({
            title: '没有数据了',
            icon: 'success',
            duration: 2000
          })

        } else {
          //分页
          var goods_list = that.data.goods_list;
          for (var i = 0; i < res.data.length; i++) {
            goods_list.push(res.data[i])
          }

          that.setData({
            goods_list: goods_list,
            page: page + 1,
            is_show: false,
          })
          wx.hideLoading();
        }
      }



      



    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

// 获取店铺信息 (有店铺信息 返回信息，没有店铺 返回)
function getshop(that, user_id) {
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Shop/shop_data.html',
    data: {
      user_id: user_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == -1) {
        that.setData({
          shop_id: 0,
        })
        wx.showModal({
          title: '提示',
          content: '您还没有提交店铺资料，请去完善资料吧！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        //返回数据值
        that.setData({
          shop_id: res.data.shop_id,
          shop_name: res.data.shop_name,
          shop_desc: res.data.shop_desc,
          collect_sum: res.data.collect_sum,
          goods_count: res.data.goods_count,
        })

        if (res.data.shop_avatar == '' || res.data.shop_avatar == null) {
        } else {
          that.setData({
            shop_avatar: res.data.shop_avatar,
          })
        }
        //获取店铺下商品集合
        goods_list(that, res.data.shop_id, 1, that.data.page_size)
      }
    },
    fail: function () { },
    complete: function () { }
  })
}